<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace common\behavior;

use common\service\EntityService;
use think\module\Module;

/**
 * 行为扩展：处理多集实体创建关联关系
 */
class DesignMultiEpisodeCreateCheckBehavior
{

    /**
     * 概念多集创建 增加关联
     * @param $data
     * @throws \Exception
     */
    public function run(&$data)
    {
        if (isset($data['new']['module_id']) && $data['new']['module_id'] === Module::$moduleDictData['module_index_by_code']['design']['id']) {
            $entityService = EntityService::getInstance();
            if (isset($data['new']['json'])) {
                if (is_string($data['new']['json'])) {
                    try {
                        $data['new']['json'] = json_decode($data['new']['json'] ?? '{}', true);
                    } catch (\Throwable $e) {
                        trace(format_exception_info($e), 'ERR');
                        $data['new']['json'] = [];
                    }
                }
                if (is_array($data['new']['json'])) {
                    $data['new'] = $data['new'] + $data['new']['json'];
                }

            }
            APP_DEBUG && trace("push check Design multi episode check" . \request()->getBatchNumber(), "DEBUG");
            $entityService->designMultiEpisodeCreateCheck($data);
        }
    }
}
